<!--
 * @Version: 1.0
 * @Autor:
 * @Date: 2021-03-10 16:57:07
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-07-06 18:20:40
 * @Description:
-->
<template>
  <el-card class="app-container content_box" shadow="always">
    <div>
      <!-- <div class="header_title_box">
        <h2>客户管理</h2>
      </div> -->
      <!-- <div class="select_form_box">
        <div class="filter-container">
          <el-input
            v-model="queryParam[select]"
            size="small"
            placeholder="请输入内容"
            class="input-with-select"
            style="width: 400px !important;"
            @keyup.enter.native="handleSearch"
          >
            <el-select slot="prepend" v-model="select" class="select" placeholder="请选择" clearable>
              <el-option label="客户编码" value="code" />
              <el-option label="客户名称" value="name" />
              <el-option label="业务员名称" value="salesman_username" />
            </el-select>
          </el-input>
          <el-select v-model="queryParam.is_status" size="small" placeholder="请选择状态" class="search" clearable @change="handleSearch">
            <el-option v-for="(item, index) in option" :key="index" :value="item.value" :label="item.label" />
          </el-select>
        </div>
      </div> -->
      <form-selections :is-active="isActive" :items="formItems" :query-param.sync="queryParam" display="block" @handleSearch="handleSearch" />
      <div class="select_flex">
        <div class="operation_button_box">
          <el-button v-action="'company@save'" icon="el-icon-circle-plus" size="small" type="success" @click="handleCreate()">新增客户</el-button>
          <el-button v-action="'company@disable'" :disabled="selectedIds.length?false:true" size="small" type="danger" @click="handleDisable()">禁用</el-button>
          <el-button v-action="'company@enable'" :disabled="selectedIds.length?false:true" size="small" type="primary" @click="handleEnable()">启用</el-button>
        </div>
        <div>
          <el-button size="small" type="primary" class="filter-item search" icon="el-icon-search" @click="handleSearch">
            搜索
          </el-button>
          <el-button size="small" icon="el-icon-refresh" @click="handleRefresh">重置</el-button>
        </div>
      </div>
      <div v-loading="loading">
        <el-table ref="multipleTable" :data="data" tooltip-effect="dark" style="width: 100%" :max-height="screenHeigh" border fit @selection-change="handleSelectMulti">
          <el-table-column align="center" type="selection" width="55" />
          <el-table-column label="操作" align="center" fixed width="200px">
            <template slot-scope="scope">
              <span v-if="scope.row.is_status===2" v-action="'company@update'" class="edit_style" @click="handleUpdateNew(scope.row)">编辑</span>
              <span v-if="scope.row.is_status===2" v-action="'company@delete'" class="delete_style" @click="handleDelete(scope.row.id)">删除</span>
              <!-- <span v-action="'shop@bindWarehouse'" class="edit_style" @click="showBindWarehouseDialog(scope.row.id)">绑定仓库</span> -->
              <span v-if="scope.row.is_status === 2" v-action="'company@accountCreated'" class="edit_style" @click="handleSetAccount(scope.row)">设置账号</span>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" label="ID" prop="id" /> -->
          <el-table-column align="center" label="客户名称" prop="name" width="240">
            <template slot-scope="scope">
              <code-item :code="scope.row.name" @click="showInfoDialog(scope.row.id)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="客户代码" prop="code" />
          <el-table-column align="center" label="客户类型" prop="type">
            <template slot-scope="scope">
              {{ scope.row.type===1?'代仓储': scope.row.type===2 ? '代运营': '自营' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="客户分类" prop="user_type">
            <template slot-scope="scope">
              {{ scope.row.user_type===1? '外部客户': '内部客户' }}
            </template>
          </el-table-column>
          <!-- amount -->
          <el-table-column align="center" label="总额度" prop="amount" width="110" />
          <el-table-column align="center" label="剩余额度" prop="overage_amount" width="110" />
          <!-- <el-table-column align="center" label="联系人" prop="contacts" />
          <el-table-column align="center" label="联系人手机号" prop="mobile" width="150px" />
          <el-table-column align="center" label="业务员" prop="salesman_username" /> -->
          <el-table-column align="center" label="登录账号状态" prop="amount">
            <template slot-scope="scope">
              {{ scope.row.account_id ? '已设置' : '未设置' }}
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" label="总额度" prop="amount" />
          <el-table-column align="center" label="剩余" prop="overage_amount" /> -->
          <el-table-column align="center" prop="is_status" label="状态">
            <template slot-scope="scope">
              {{ scope.row.is_status===1?'启用':'禁用' }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="creator_name" label="创建人" width="120" />
          <el-table-column align="center" prop="created_at" label="创建时间" width="165" />
          <el-table-column align="center" prop="update_name" label="修改人" width="120">
            <template slot-scope="scope">
              {{ scope.row.update_name?scope.row.update_name:'-' }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="updated_at" label="修改时间" width="165" />
        </el-table>
        <el-pagination v-if="paginate.total > 0" class="pagination_box" background :current-page="paginate.current" :page-sizes="[10, 20, 30, 40]" layout="total, sizes, prev, pager, next, jumper" :total="paginate.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
      <!-------- 回到顶部 ------------>
      <el-backtop :visibility-height="60">
        <i class="el-icon-caret-top" />
      </el-backtop>
      <el-dialog :center="true" :visible.sync="formVisible" width="950px" @close="handleCancel">
        <el-form :ref="formName" :model="formFieldsData" :rules="rules" inline class="dialog-form-expand2" :disabled="isInfo">
          <div class="header_title_box">
            <h2>客户信息</h2>
          </div>
          <el-form-item label="客户代码" :label-width="formLabelWidth" prop="code">
            <el-input v-model.trim="formFieldsData.code" oninput="value=value.replace(/[\u4E00-\u9FA5]/g,'')" placeholder="请输入客户代码" autocomplete="off" clearable style="width:247px" />
          </el-form-item>
          <el-form-item label="客户" :label-width="formLabelWidth" prop="name">
            <el-input v-model.trim="formFieldsData.name" placeholder="请输入客户" autocomplete="off" clearable style="width:247px" />
          </el-form-item>
          <el-form-item label="客户类型" :label-width="formLabelWidth" prop="type">
            <el-select v-model="formFieldsData.type" placeholder="请选择" style="width:247px" clearable>
              <el-option label="代仓储" :value="1" />
              <el-option label="代运营" :value="2" />
              <el-option label="自营" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label="客户分类" :label-width="formLabelWidth" prop="user_type">
            <el-select v-model="formFieldsData.user_type" placeholder="请选择" style="width:247px" clearable>
              <el-option label="外部客户" :value="1" />
              <el-option label="内部客户" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label="联系人" :label-width="formLabelWidth" prop="contacts">
            <el-input v-model.trim="formFieldsData.contacts" placeholder="请输入联系人" autocomplete="off" clearable style="width:247px" />
          </el-form-item>
          <el-form-item label="手机号码" :label-width="formLabelWidth" prop="mobile">
            <el-input v-model.trim="formFieldsData.mobile" maxlength="11" placeholder="请输入手机号码" autocomplete="off" clearable style="width:247px" />
          </el-form-item>
          <el-form-item label="座机" :label-width="formLabelWidth" prop="telephone">
            <el-input v-model.trim="formFieldsData.telephone" placeholder="请输入座机" autocomplete="off" clearable style="width:247px" />
          </el-form-item>
          <el-form-item label="业务员" :label-width="formLabelWidth" prop="salesman_username">
            <el-input v-model.trim="formFieldsData.salesman_username" placeholder="请输业务员" autocomplete="off" clearable style="width:247px" />
          </el-form-item>
          <el-form-item label="银行名称" :label-width="formLabelWidth" prop="bank_name">
            <el-input v-model.trim="formFieldsData.bank_name" placeholder="请输入银行名称" autocomplete="off" clearable style="width:247px" />
          </el-form-item>
          <el-form-item label="银行账号" :label-width="formLabelWidth" prop="bank_number">
            <el-input v-model.trim="formFieldsData.bank_number" placeholder="请输入银行账号" autocomplete="off" clearable style="width:247px" />
          </el-form-item>
          <el-form-item label="传真" :label-width="formLabelWidth" prop="fax">
            <el-input v-model.trim="formFieldsData.fax" placeholder="请输入传真" autocomplete="off" clearable style="width:247px" />
          </el-form-item>
          <el-form-item label="邮编" :label-width="formLabelWidth" prop="zip_code">
            <el-input v-model.trim="formFieldsData.zip_code" placeholder="请输入邮编" autocomplete="off" clearable style="width:247px" />
          </el-form-item>
          <el-form-item label="地址" :label-width="formLabelWidth" prop="address">
            <el-input v-model.trim="formFieldsData.address" placeholder="请输入地址" autocomplete="off" clearable style="width:247px" />
          </el-form-item>
          <el-form-item label="备注说明" :label-width="formLabelWidth" prop="remarks">
            <el-input v-model="formFieldsData.remarks" type="textarea" placeholder="请输入备注说明" autosize show-word-limit autocomplete="off" clearable style="width:247px" />
          </el-form-item>
        </el-form>
        <div class="header_title_box">
          <h2 style="margin-right:50px">
            账户信息
          </h2>
          <div v-if="!isInfo">
            <el-button type="primary" size="mini" icon="el-icon-plus" @click="addField">新增</el-button>
          </div>
        </div>
        <div>
          <el-form ref="formData" :rules="model.rules" :model="model" class="dialog-form-expand3" :disabled="isInfo">
            <el-table :data="model.fields" tooltip-effect="dark" style="width: 100%" border>
              <el-table-column align="center" label="操作" width="150">
                <template slot-scope="field">
                  <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteField(field.row, 'company_quota')" />
                  <el-button v-if="!field.row.disabled && formFieldsData.id" type="primary" size="mini" @click="saveCurrency(formFieldsData.id, field.row, field.$index, 'company_quota')">保存
                  </el-button>
                </template>
              </el-table-column>
              <el-table-column align="center" label="币别">
                <template slot-scope="field">
                  <el-form-item v-if="!isInfo" :prop="'fields.' + field.$index + '.currency_data'" :rules="model.rules.currency_data">
                    <el-select v-model="field.row.currency_data" :disabled="field.row.disabled" size="mini" filterable clearable placeholder="请输入关键词">
                      <el-option v-for="item in optionsCurrency" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                  </el-form-item>
                  <el-input v-else v-model="field.row.currency_name" size="small" />
                </template>
              </el-table-column>
              <el-table-column align="center" label="额度">
                <template slot-scope="field">
                  <el-form-item :prop="'fields.' + field.$index + '.quota'" :rules="model.rules.quota">
                    <el-input v-model.trim="field.row.quota" :disabled="field.row.disabled" size="mini" placeholder="输入额度" autocomplete="off" clearable maxlength="10" />
                  </el-form-item>
                </template>
              </el-table-column>
            </el-table>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer-center">
          <el-button :type="isInfo ? 'primary' : 'default'" @click="handleCancel">{{ isInfo ? '返回' : '取消' }}</el-button>
          <el-button v-if="!isInfo" type="primary" @click="handleSubmitNew">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 设置账户 -->
      <el-dialog :center="true" title="设置账户" :visible.sync="formVisibleAccount" width="850px" @close="handleCancelAccount">
        <el-form :ref="formNameAccount" v-loading="resetAccountLoading" :model="resetFormFieldsAccount" :rules="rules" inline class="dialog-form-expand2">
          <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
            <el-input v-model.trim="resetFormFieldsAccount.username" size="mini" :disabled="!!resetFormFieldsAccount.already" placeholder="请输入用户名" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
            <el-input v-model.trim="resetFormFieldsAccount.email" size="mini" placeholder="请输入邮箱" :disabled="!!resetFormFieldsAccount.already" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
            <el-input v-model.trim="resetFormFieldsAccount.password" size="mini" placeholder="请输入密码" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="手机号码" :label-width="formLabelWidth" prop="phone">
            <el-input v-model.trim="resetFormFieldsAccount.phone" maxlength="11" size="mini" placeholder="请输入手机号码" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
            <el-input v-model.trim="resetFormFieldsAccount.name" size="mini" placeholder="请输入姓名" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="备注" :label-width="formLabelWidth" prop="remarks">
            <el-input v-model.trim="resetFormFieldsAccount.remarks" size="mini" placeholder="请输入备注" autocomplete="off" clearable />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer-center">
          <el-button @click="handleCancelAccount()">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 绑定仓库 -->
      <bind-warehouse-dialog ref="warehouse" @submit="handleRefresh" />
    </div>
  </el-card>

</template>

<script>
import formOperate from '@/layout/mixin/formOperate'
import { action } from '@/directive/permission/index.js' // 权限判断指令
import { currencyAll } from '@/utils/publicData'
import { isPoneAvailable, validChineseOrs } from '@/utils/validate'
import codeItem from '@/components/base/code'
import BindWarehouseDialog from './components/bindWarehouseDialog'
import cacheList from '@/layout/mixin/cacheList'
export default {
  name: 'BascicsCompany',
  components: { codeItem, BindWarehouseDialog },
  directives: {
    action
  },
  mixins: [formOperate, cacheList],
  data() {
    return {
      otherHeight: 365,
      componentName: 'BascicsCompany',
      id: '',
      resetFormFieldsAccount: {
        email: '',
        username: '',
        password: '',
        phone: '',
        roles: [3],
        name: '',
        remarks: '',
        already: ''
      },
      formNameAccount: 'userAccount',
      formVisibleAccount: false,
      url: '/company',
      queryParam: {
        is_status: '',
        name: '',
        type: '',
        code: '',
        salesman_username: ''
      },
      formItems: [
        {
          label: '客户编码',
          key: 'code',
          type: 'input',
          placeholder: '请输入客户编码'
        },
        {
          label: '客户名称',
          key: 'name',
          type: 'input',
          placeholder: '请输入客户名称'
        },
        {
          label: '业务员名称',
          key: 'salesman_username',
          type: 'input',
          placeholder: '请输入业务员名称'
        },
        {
          label: '状态',
          key: 'is_status',
          type: 'select',
          placeholder: '请选择状态',
          options: [
            {
              value: 2,
              label: '禁用'
            },
            {
              value: 1,
              label: '启用'
            }
          ]
        }
      ],
      select: 'code',
      defaultQueryParam: ['type'],
      option: [
        {
          value: 2,
          label: '禁用'
        },
        {
          value: 1,
          label: '启用'
        }
      ],
      loading: false,
      formVisible: false,
      formFieldsData: {
        code: '', // 店铺编号
        name: '',
        type: '', // 客户仓库类型，1：代仓储，0：自营
        user_type: '', // 1：外部客户，0：内部客户
        contacts: '',
        mobile: '',
        telephone: '',
        salesman_username: '',
        bank_name: '',
        bank_number: '',
        fax: '',
        zip_code: '',
        address: '',
        remarks: ''
      },
      formName: 'companys',
      formLabelWidth: '150px',
      rules: {
        username: [
          { required: true, message: '请输入用户', trigger: 'blur' },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '邮箱地址格式不正确', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          {
            validator(rule, value, callback) {
              isPoneAvailable(value)
                ? callback(new Error('请输入正确的手机号码'))
                : callback()
            },
            trigger: 'blur'
          }
        ],
        realname: [
          { required: true, message: '请输入真实名字', trigger: 'blur' },
          {
            min: 2,
            max: 20,
            message: '长度在 2 到 20 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^[0-9a-zA-Z@_]{8,16}$/,
            message: '8-16位，数字、大写字母、小写字母组成',
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入客户代码', trigger: 'blur' },
          {
            validator(rule, value, callback) {
              if (validChineseOrs(value)) {
                callback(new Error('代码须是字母，数字，非空字符组成'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        name: [{ required: true, message: '请输入客户名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择', trigger: 'change' }],
        user_type: [{ required: true, message: '请选择', trigger: 'change' }],
        mobile: [
          {
            validator(rule, value, callback) {
              value
                ? isPoneAvailable(value)
                  ? callback(new Error('请输入正确的手机号码'))
                  : callback()
                : callback()
            },
            trigger: 'blur'
          }
        ],
        telephone: [
          { max: 30, message: '座机必须在30个字符以内', trigger: 'blur' }
        ],
        salesman_username: [
          { max: 30, message: '业务员名称必须在30个字符以内', trigger: 'blur' }
        ],
        bank_number: [
          {
            pattern: /^[0-9a-zA-Z]*$/,
            message: '银行账号必须为数字或字母组成',
            trigger: 'blur'
          },
          { max: 50, message: '银行账号必须在50个字符以内', trigger: 'blur' }
        ],
        fax: [{ max: 20, message: '传真必须在20个字符以内', trigger: 'blur' }],
        zip_code: [
          { max: '20', message: '邮编必须在20个字符以内', trigger: 'blur' }
        ],
        address: [
          { max: 100, message: '地址必须在100个字符以内', trigger: 'blur' }
        ]
      },
      model: {
        rules: {
          currency_data: {
            required: true,
            message: '必填字段',
            trigger: ['change', 'blur']
          },
          quota: { required: true, message: '必填字段', trigger: 'blur' }
        },
        fields: [this.getColumn()]
      },
      optionsCurrency: [],
      isInfo: false,
      resetAccountLoading: false
    }
  },
  created() {
    const vm = this
    currencyAll().then((data) => {
      vm.optionsCurrency = data
      console.log('========vm.optionsCurrency=====', vm.optionsCurrency)
    })
  },
  methods: {
    // 显示绑定 dialog
    showBindWarehouseDialog(id) {
      const vm = this
      vm.$refs.warehouse.show(id)
    },
    saveCurrency(id, data, index, url) {
      const vm = this
      if (!vm.checkCurrencyIsSame()) {
        return this.$message.error('添加账号的币别必须是同一个')
      }
      vm.save(id, data, index, url)
    },
    // 校验 币别是否一致
    checkCurrencyIsSame() {
      const firstCurrency = this.model.fields[0].currency_data
      const every = this.model.fields.every(
        (e) => e.currency_data === firstCurrency
      )
      return every
    },
    // 显示 info dialog
    showInfoDialog(id) {
      const vm = this
      vm.isInfo = true
      vm.$http.get(`/company/${id}`).then((res) => {
        vm.formFieldsData.code = res.data.code // 店铺编号
        vm.formFieldsData.name = res.data.name
        vm.formFieldsData.type = res.data.type // 客户仓库类型，1：代仓储，0：自营
        vm.formFieldsData.user_type = res.data.user_type // 1：外部客户，0：内部客户
        vm.formFieldsData.contacts = res.data.contacts
        vm.formFieldsData.mobile = res.data.mobile
        vm.formFieldsData.telephone = res.data.telephone
        vm.formFieldsData.salesman_username = res.data.salesman_username
        vm.formFieldsData.bank_name = res.data.bank_name
        vm.formFieldsData.bank_number = res.data.bank_number
        vm.formFieldsData.fax = res.data.fax
        vm.formFieldsData.zip_code = res.data.zip_code
        vm.formFieldsData.address = res.data.address
        vm.formFieldsData.remarks = res.data.remarks
        vm.model.fields = res.data.companyQuotaList
      })
      vm.formVisible = true
    },
    // 设置账号
    handleSetAccount(item) {
      const vm = this
      vm.id = item.id
      if (item.account_id) {
        vm.resetAccountLoading = true
        vm.$http
          .get(`/company/${item.id}`)
          .then((res) => {
            const userItem = res.data.userData.find(
              (e) => e.id === item.account_id
            )
            const { username, name, remarks, phone, email } = userItem
            vm.resetFormFieldsAccount.email = email
            vm.resetFormFieldsAccount.username = username
            vm.resetFormFieldsAccount.phone = phone
            vm.resetFormFieldsAccount.name = name
            vm.resetFormFieldsAccount.already = 1
            vm.resetFormFieldsAccount.remarks = remarks
            vm.resetAccountLoading = false
          })
          .catch(() => {
            vm.resetAccountLoading = false
          })
      } else {
        Object.keys(vm.resetFormFieldsAccount).forEach((k) => {
          vm.resetFormFieldsAccount[k] = ''
        })
      }
      vm.formVisibleAccount = true
    },
    // 关闭
    handleCancelAccount() {
      const vm = this
      vm.formVisibleAccount = false
      // 防止提交关闭的时候 导致数据清空 所以延迟 300ms 清空数据
      vm.resetFormFieldsAccount.email = ''
      vm.resetFormFieldsAccount.username = ''
      vm.resetFormFieldsAccount.password = ''
      vm.resetFormFieldsAccount.phone = ''
      vm.resetFormFieldsAccount.name = ''
      vm.resetFormFieldsAccount.already = ''
      vm.resetFormFieldsAccount.remarks = ''
      vm.$refs[vm.formNameAccount].clearValidate()
    },
    // 新增之前
    beforeCreate() {
      this.formFieldsData = {}
      this.model.fields = []
    },
    handleUpdateNew(data) {
      this.$http.get(`company/${data.id}`).then((res) => {
        const dataObj = res.data
        this.formFieldsData = dataObj
        this.formFieldsData.zip_code = `${dataObj.zip_code}`
        if (
          dataObj.companyQuotaList &&
          dataObj.companyQuotaList.length > 0 &&
          dataObj.companyQuotaList[0].quota
        ) {
          this.model.fields = dataObj.companyQuotaList.map((arr) => {
            arr.disabled = true
            arr.currency_data = arr.currency_id + ',' + arr.currency_name
            return arr
          })
        }
        this.formVisible = true
      })
    },
    // 新增绑定账户
    submit() {
      this.$refs['userAccount'].validate((valid) => {
        if (valid) {
          this.$http
            .put(
              `companys/accountCreated/${this.id}`,
              this.resetFormFieldsAccount
            )
            .then((response) => {
              if (response.code === 10000) {
                this.$message.success('账号设置成功')
                this.formVisibleAccount = false
                this.handleRefresh()
              } else {
                this.$message.error(response.message)
              }
            })
        }
      })
    },
    handleSubmitNew() {
      this.$refs['companys'].validate((valid) => {
        if (valid) {
          if (this.model.fields <= 0) {
            return this.$message.error('请添加至少一个账号')
          }
          if (!this.checkCurrencyIsSame()) {
            return this.$message.error('添加账号的币别必须是同一个')
          }
          // 编辑
          if (this.formFieldsData.id) {
            this.$http
              .put(`company/${this.formFieldsData.id}`, this.getData())
              .then((response) => {
                if (response.code === 10000) {
                  this.$message.success('编辑成功')
                  this.formVisible = false
                  this.handleRefresh()
                } else {
                  this.$message.error(response.message)
                }
              })
          } else {
            // 判断个人信息
            this.$refs['formData'].validate((validSon) => {
              // 添加
              if (validSon) {
                this.$http.post('company', this.getData()).then((response) => {
                  if (response.code === 10000) {
                    this.$message.success('添加成功')
                    this.formVisible = false
                    this.handleRefresh()
                  } else {
                    this.$message.error(response.message)
                  }
                })
              }
            })
          }
        }
      })
    },
    // 获取添加参数组合
    getData() {
      const data = Object.assign({}, this.formFieldsData)
      delete data.updated_at
      delete data.created_at
      data['is_status'] = 1
      if (this.model.fields && this.model.fields.length > 0) {
        const dataArr = this.model.fields.map((arr) => {
          arr.currency_arr = arr.currency_data.split(',')
          return {
            currency_id: arr.currency_arr[0],
            currency_name: arr.currency_arr[1],
            quota: arr.quota
          }
        })
        data['datajson'] = JSON.stringify(dataArr)
      }
      return data
    },
    // 数据列表
    getColumn() {
      return {
        currency_data: '',
        quota: ''
      }
    },
    list() {
      this.senderList()
    },
    // 寄件人信息列表
    senderList() {
      const vm = this
      const data = {
        limit: 100,
        page: 1,
        id: this.formFieldsData.id
      }
      vm.$http
        .get('company_quota', {
          params: data
        })
        .then((res) => {
          if (res.code === 10000) {
            this.model.fields = res.data.map((arr) => {
              arr.disabled = true
              arr.currency_data = arr.currency_id + ',' + arr.currency_name
              return arr
            })
          }
        })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
